<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="学号">
              <a-input v-model="queryParam.xh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="导师姓名">
                <a-input v-model="queryParam.dsxm" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="第二导师姓名">
                <a-input v-model="queryParam.dedsxm" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.ssyxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="专业">
                <a-select :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.sszym">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select :allowClear="true" v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="年级">
                <a-select :allowClear="true" v-model="queryParam.sznj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="入学方式">
                <a-select :allowClear="true" v-model="queryParam.rxfs">
                  <a-select-option v-for="i in TYPE0064" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学籍状态">
                <a-select :allowClear="true" v-model="queryParam.xjztm">
                  <a-select-option v-for="i in TYPE0073" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学业奖学金等级">
                <a-select :allowClear="true" v-model="queryParam.xyjxjdjm">
                  <a-select-option v-for="i in TYPE0311" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="助研奖学金等级">
                <a-select :allowClear="true" v-model="queryParam.zyjxjdjm">
                  <a-select-option v-for="i in TYPE0312" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="录取类别">
                <a-select :allowClear="true" v-model="queryParam.lqlbm">
                  <a-select-option v-for="i in TYPE0362" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学制">
                <a-select :allowClear="true" v-model="queryParam.xz">
                  <a-select-option v-for="i in TYPE0301" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <transfer-form
      ref="transferForm"
      :visible.sync="visible"
      :transferInfo.sync="transferInfo"
      :yx.sync="selectMenu.yx"
      @close="visible = false"
      @refresh="refreshTable"
    />

    <!-- <tutor-form
      ref="tutorForm"
      :tutorVisible.sync="tutorVisible"
      :transferId.sync="transferId"
      @close="tutorVisible = false"
      @refresh="refreshTable"
    /> -->
      <!-- @refresh="refreshTable" -->

    <s-table
      v-if="initDone"
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="{show: true}"
      :pageSize="parameter.pageSize"
      :pageNum="parameter.pageNum"
      :scroll="{x: 1600}"
    >

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
      </span>
      <template slot="xh" slot-scope="a,b">
        <a @click="direct(b)">{{a}}</a>
      </template>
      <template slot="operation" slot-scope="a, b">
        <a @click="checkTransfer(b)">学籍异动</a>
        <!-- <a-divider type="vertical" /> -->
        <!-- <a @click="toTutorChange(b)">导师异动</a> -->
      </template>
    </s-table>

  </a-card>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import transferForm from './transferForm'
import tutorForm from './tutorForm'

export default {
  name: 'TableList',
  components: {
    STable,
    transferForm,
    tutorForm
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      visible: false,
      transferInfo: null,
      tutorVisible: false,
      transferId: null,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '学号',
          dataIndex: 'xh',
          scopedSlots: { customRender: 'xh' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '专业',
          dataIndex: 'sszymc'
        },
        {
          title: '年级',
          dataIndex: 'sznj'
        },
        {
          title: '导师姓名',
          dataIndex: 'dsxm'
        },
        {
          title: '证件号码',
          dataIndex: 'zjhm'
        },
        {
          title: '学生类别',
          dataIndex: 'xslbmc'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width:90,
          fixed:'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        if (this.queryParam.createTimeFrom) {
          this.queryParam.createTimeFrom = this.queryParam.createTimeFrom.format('YYYY-MM-DD')
        }
        if (this.queryParam.createTimeTo) {
          this.queryParam.createTimeTo = this.queryParam.createTimeTo.format('YYYY-MM-DD')
        }
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.student.stuStatusList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRows: [],
      options: {
        alert: { show: false }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      confirmLoading: false,
      selectMenu: {
        yx: []
      },
      initDone: false
    }
  },
  computed: {
    ...mapState({
      TYPE0311: state => state.app['TYPE0311'], // 学业奖学金等级
      TYPE0312: state => state.app['TYPE0312'], // 助研奖学金等级
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0075: state => state.app['TYPE0075'], // 学生类别
      TYPE0362: state => state.app['TYPE0362'], // 录取类别
      TYPE0064: state => state.app['TYPE0064'], // 入学方式
      TYPE0073: state => state.app['TYPE0073'], // 学籍状态
      TYPE0301: state => state.app['TYPE0301'] // 学制
    })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      let pageParams = vm.$store.state.app.pageParams || {}
      if (from.path === '/student/stuStatus/search/stuView' && pageParams.path === vm.$route.path) {
        vm.queryParam = pageParams.params
        vm.parameter = pageParams.pagination
      }
      vm.initSelectMenu()
    })
  },
  beforeRouteLeave (to, from, next) {
    if (to.path === '/student/stuStatus/search/stuView') {
      this.$store.dispatch('app/setPageParams', {
        path: this.$route.path,
        params: this.queryParam,
        pagination: this.parameter
      })
    }
    next()
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0064')
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0311')
    this.$store.dispatch('app/setDictionary', 'TYPE0312')
    this.$store.dispatch('app/setDictionary', 'TYPE0362')
    this.$store.dispatch('app/setDictionary', 'TYPE0073')
    this.$store.dispatch('app/setDictionary', 'TYPE0301')
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        this.initDone = true
      })
    },
    resetForm () {
      this.queryParam = {}
      this.selectMenu.zy = []
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    direct (data) {
      this.$router.push(`/student/stuStatus/search/stuView?xh=${data.xh}`)
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    checkTransfer (item) {
      this.$api.student.transferingInfo(item.xh).then(res => {
        this.transferInfo = res.data
      })
      this.visible = true
    },
    toTutorChange (item) {
      this.tutorVisible = true
      this.transferId = item.id
      // this.$router.push(`/student/change/tutorChange?id=${item.id}`)
    },
    refreshTable () {
      this.$refs.table.refresh(false)
    }
    // saveTutor () {
    //   this.$refs.tutorForm.form.validateFields((err, values) => {
    //     if (err) {
    //       return
    //     }
    //     this.loading = true
    //     let params = Object.assign(this.studentInfo, values, {xjydId: this.d})
    //     for (const key in params) {
    //       if (['xcsrq', 'xbyny', 'ydrq', 'createDate'].includes(key) && params[key]) {
    //         params[key] = params[key].format('YYYY-MM-DD hh:mm:ss')
    //       }
    //     }
    //     this.$api.student.transferingSave(params).then(res => {
    //       this.$message.success(res.msg)
    //     }).finally(() => {
    //       this.loading = false
    //     })
    //   })
    // }
  }
}
</script>
